﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vukhoas.src.POJO;
using System.Data.SqlClient;
using vukhoas.src;
using System.IO;

namespace vukhoas.VukhoasAdmin
{
    public partial class EditProduct : System.Web.UI.Page
    {
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = int.Parse(Request.QueryString["id"]);
            if(!IsPostBack)
            pageload();
        }

        private bool update(){
            bool flag = false;
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "UPDATE product SET name=@name,price=@price,manufacture_id=@manu,category_id=@cate,detail=@detail,introduce=@intro,information=@info,image=@img,created_date=@date WHERE id="+id;
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@name", name.Text);
                cmd.Parameters.AddWithValue("@price",price.Text);
                cmd.Parameters.AddWithValue("@manu", manu.SelectedValue);
                cmd.Parameters.AddWithValue("@cate", cate.SelectedValue);
                cmd.Parameters.AddWithValue("@detail", detail.Text);
                cmd.Parameters.AddWithValue("@intro", intro.Text);
                cmd.Parameters.AddWithValue("@info", info.Text);
                cmd.Parameters.AddWithValue("@img", img.Text);
                cmd.Parameters.AddWithValue("@date", DateTime.Now);
                cmd.ExecuteNonQuery();
                flag = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return flag;

        }

        private void pageload(){
            name.Text = product().Name;
            price.Text = product().Price.ToString() ;
            cate.SelectedValue = product().CategoryID;
            manu.SelectedValue = product().ManufactureID;
            detail.Text = product().Detail;
            intro.Text = product().Introduce;
            info.Text = product().Information;
            img.Text = product().Image;
        }

        protected Product product()
        {
            Product pro = new Product();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM product WHERE id = " + id;
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    pro.Id = dr.GetInt32(0);
                    pro.Name = dr.GetString(1);
                    pro.Price = dr.GetString(2);
                    pro.ManufactureID = dr.GetString(3);
                    pro.CategoryID = dr.GetString(4);
                    pro.Detail = dr.GetString(5);
                    pro.Introduce = dr.GetString(6);
                    pro.Information = dr.GetString(7);
                    pro.Image = dr.GetString(8);
                    pro.CreatedDate = dr.GetDateTime(9);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return pro;
        }

        protected void ok_Click(object sender, EventArgs e)
        {
            if(update())
            {
                Alert.Show("Sản phẩm đã được cập nhật thành công");
               // Response.Redirect("ListProduct.aspx");
            }
        }

        protected void reset_Click(object sender, EventArgs e)
        {

        }

        protected void cate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string file = "/data/template/";
            switch (cate.SelectedValue)
            {
                case "macbook":
                    file += "laptop.htm";
                    break;
                case "iphone":
                    file += "phone.htm";
                    break;
                case "smartphone":
                    file += "phone.htm";
                    break;
                case "phd":
                    file += "phd.htm";
                    break;
                case "ipad":
                    file += "tablet.htm";
                    break;
            }
            StreamReader sr = File.OpenText(Server.MapPath(file));
            detail.Text = sr.ReadToEnd();
        }
    }
}